import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { ShudleService } from '../../service/shudleservice/shudle.service';
import { HttpService } from '../../service/http.service';

@Component({
  selector: 'app-scheduling',
  templateUrl: './scheduling.component.html',
  styleUrls: ['./scheduling.component.css']
})

export class SchedulingComponent implements OnInit {

  regtype: number;
  deptid: number;

  doctorurl = 'hospital!getDoctorListById?d_id=';
  doctorlists: any;
  schedulingurl = 'hospital!getArrangmentInfoById?d_d_id=';
  schedulinglist: any;
  hospid: string;
  doctor: any;
  shudleList: any;
  shudle: any;
  time1: any[] = [];
  time2: any[] = [];
  idx = 1;
  indexi = 0;
  indexj = 0;
  cho2 = true;
  cho1: number;
  mtype: boolean;
  shudata: any;

  constructor(private routerdata: ActivatedRoute, private shudledata: ShudleService, private listdata: HttpService, private router: Router) {
  }

  ngOnInit() {
    this.routerdata.params.subscribe((params: Params) => { this.hospid = params['id'], this.deptid = params['deptid'], this.regtype = params['regtype']; });
    this.getDoctorList();
    this.getSchedulingList();
    this.shudleList = this.shudledata.shudleList;
    this.mtype = false;
    console.log('hospid:', this.hospid);
    console.log('deptid:', this.deptid);
    for (let i = 0; i < this.shudleList.length; i++) {
      const shudleOne = this.shudleList[i];
      if (shudleOne.hospId == this.hospid && shudleOne.deptCode == this.deptid) {
        this.shudle = shudleOne.shulde;
        let l = 0;
        const data1 = this.shudle;
        this.cho1 = Math.ceil(data1.length / 4) - 1;
        console.log('cho1:', this.cho1);
        for (let g = 0; g < data1.length / 4; g++) {
          const b = [];
          for (let j = 0; j < 4; j++) {
            if (data1[l] != null) {
              b.push(data1[l]);
            } else {
              b.push([]);
            }
            l++;
          }

          this.time1.push(b);
        }

        console.log('shudleone:', this.time1);
      }
    }
    this.shudata = this.time1;
    this.doctor = this.shudle[0].DoctorList;
  }
  //获取医生数据
  getDoctorList() {
    this.listdata.httpGet(this.doctorurl+'2', {}).subscribe(res => {
      this.doctorlists = res[0];
      console.log('接口名称:'+this.doctorurl, this.doctorlists);
    });
  }
  //获取医生排班数据
  getSchedulingList() {
    this.listdata.httpGet(this.schedulingurl+'1', {}).subscribe(res => {
      this.schedulinglist = res[0];
      console.log('接口名称:'+this.schedulingurl, this.schedulinglist);
    });
  }

  doctorIndex() {
    this.router.navigate(['/doctorhome']);
  }

  previous(index: number) {
    this.idx = index;
    console.log('index:', index);
  }

  next(index: number) {
    this.idx = index;
    console.log('index:', index);
  }

  itemClick(i: number, j: number) {
    this.indexi = i;
    this.indexj = j;
    console.log('i:', i, 'j:', j);
  }

  doctorlist(y: number) {
    console.log('y:', y);
    for (let w = 0; w < this.shudle.length; w++) {
      if (this.shudle[w].shuId === y) {
        this.doctor = this.shudle[w].DoctorList;
      }
    }
  }

  sb() {
    if (this.mtype === false) {
      console.log('type=false');
      this.mtype = true;
      this.time2 = [];
      const data2 = this.shudle;
      const c = [];
      for (let p = 0; p < data2.length; p++) {
        c.push(data2[p]);
        if (data2[p].DoctorList.length === 0) {
          c.pop();
        }
        console.log('doctorName:');
      }
      this.cho1 = Math.ceil(c.length / 4) - 1;
      console.log('cho1:', this.cho1);
      let l = 0;
      for (let g = 0; g < c.length / 4; g++) {
        const d = [];
        for (let j = 0; j < 4; j++) {
          if (c[l] != null) {
            d.push(c[l]);
          } else {
            d.push([]);
          }
          l++;
        }
        this.time2.push(d);
      }
      console.log('shudletwo:', this.time2);
      this.shudata = this.time2;
    } else {
      this.cho1 = Math.ceil(this.shudle.length / 4) - 1;
      console.log('type=true');
      console.log('cho1:', this.cho1);
      this.mtype = false;
      this.shudata = this.time1;
      console.log('shudleone:', this.time1);
    }
  }

  doRegister() {
    this.router.navigate(['/appoint/contrement']);
  }
}
